/**
 * 
 */
package com.googlecode.exocom.data;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.util.Log;

import com.googlecode.exocom.ExoCom;
import com.googlecode.exocom.R;

/**
 * @author MaxDaten
 *
 */
public abstract class APIConfigurationFactory {
	enum Tag {
		API("main"), DOC("doc"), MYEVE("myeve");

		final String tagName;

		private Tag(final String s) {
			this.tagName = s;
		}

		public final String getTagName() {
			return tagName;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Enum#toString()
		 */
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return super.name() + ":" + getTagName();
		}
	}
	final static String TAG = ExoCom.TAG;

	public static APIConfiguration createConfiguration(final Resources res)
	throws XmlPullParserException, IOException {
		String api = "", doc = "", myeve = "";

		final XmlResourceParser x = res.getXml(R.xml.api);

		int eventType = x.getEventType();

		while (eventType != XmlResourceParser.END_DOCUMENT) {
			final String name = x.getName();
			if (eventType == XmlResourceParser.START_DOCUMENT) {
				Log.v(TAG, "Start document");
			} else if (eventType == XmlResourceParser.END_DOCUMENT) {
				Log.v(TAG, "End document");

				// The important tags
			} else if (eventType == XmlResourceParser.START_TAG) {

				if (name.contentEquals(Tag.API.getTagName())) {
					api = x.getAttributeValue(null, "href").trim();
				} else if (name.contentEquals(Tag.DOC.getTagName())) {
					doc = x.getAttributeValue(null, "href").trim();
				} else if (name.contentEquals(Tag.MYEVE.getTagName())) {
					myeve = x.getAttributeValue(null, "href").trim();
				} else {
					// Some other tag -> will be ignored
					if (Log.isLoggable(TAG, Log.VERBOSE)) {
						Log.v(TAG, "unknown element : " + name);
					}
				}

			} else if (eventType == XmlResourceParser.END_TAG) {
			} else if (eventType == XmlResourceParser.TEXT) {
			}
			eventType = x.next();
		}


		Log.v(TAG, "Parsed: api:" + api + " doc:" + doc + " myeve:"
				+ myeve);

		x.close();

		return new APIConfiguration(api, doc, myeve);
	}

}
